
-- Some stuff (LibBars handling, positioning) taken from Heatsink/HotCandy

local function print(message) DEFAULT_CHAT_FRAME:AddMessage("|cff33ff99Wheel:|r "..tostring(message)) end

local Wheel = Wheel

local LSM = LibStub("LibSharedMedia-3.0")
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")

local LSM_Fonts = {}
local LSM_Textures = {}

-----------------------
--   Bars handling   --
-----------------------

function Wheel:CreateGroup(name, group)
	local group = group
	print("CreateAnchor: "..name.." : "..group)
	if not self.anchor then
		self.anchor = {}
	end

	self.anchor[group] = self:NewBarGroup(name, nil, self.db.profile[group].width, self.db.profile[group].height)
	self.anchor[group]:ClearAllPoints()

	self.anchor[group].RegisterCallback(self, "TimerFinished")
	self.anchor[group].RegisterCallback(self, "AnchorMoved", "SavePosition")
	self.anchor[group]:HideAnchor()
--	self.anchor[group]:SetSortFunction(self.anchor.NOOP)

--	self.anchor[group].group = group
	self:ApplySettings(group)
end

function Wheel:AnchorClicked(callback, group, button)
	self:SavePosition(group)
	if button == "RightButton" then
		group:HideAnchor()
		group:Lock()
	end
end

function Wheel:TimerFinished(callback, group, bar)
end

function Wheel:UpdateProfile()
	self:UpdateAll()
	self:UpdateLock()
end

function Wheel:UpdateLock()
	for group in pairs(self.anchor) do
		if self.db.profile[group].lock then
			self.anchor[group]:ShowAnchor()
			self.anchor[group]:Unlock()
		else
			self.anchor[group]:HideAnchor()
			self.anchor[group]:Lock()
		end
	end
end

function Wheel:LoadPosition(group)
--	local group = anchor.group
--	print("LoadPosition: "..group)
	local pos = self.db.profile[group].position
	if not pos or not pos.x or not pos.y or not pos.point or not pos.relPoint then
		self:ResetPosition(group)
		return
	end

	local x, y, s = pos.x, pos.y, self.anchor[group]:GetEffectiveScale()
	local point, relPoint = pos.point, pos.relPoint
	x, y = x / s, y / s
	self.anchor[group]:ClearAllPoints()
	self.anchor[group]:SetPoint(point, UIParent, relPoint, x, y)
end

function Wheel:SavePosition()
	for group in pairs(self.anchor) do
		local pos = {}
		local point, parent, relPoint, x, y = self.anchor[group]:GetPoint()
		local s = self.anchor[group]:GetEffectiveScale()
		x, y = x * s, y * s
		pos.x, pos.y = x, y
		pos.point, pos.relPoint = point, relPoint

		self.db.profile[group].position = pos
	end
end

function Wheel:ResetPosition(group)
--	local group = anchor.group
	self.anchor[group]:ClearAllPoints()
	self.anchor[group]:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
	self:SavePosition(anchor)
end

function Wheel:ApplySettings(group)
	local anchor = self.anchor[group]
	local db = self.db.profile[group]
	anchor:SetTexture(LSM:Fetch("statusbar", db.texture))
	anchor:SetFont(LSM:Fetch("font", db.font), db.fontsize)
	anchor:SetWidth(db.width)
	anchor:SetHeight(db.height)
	anchor:ReverseGrowth(db.growth)

	anchor:SetFlashTrigger(0)
	if db.flash then
		-- bar is up only few seconds, want to flash it all the time
		anchor:SetFlashTrigger(30)
	end

	anchor:SetScale(db.scale)

	self:LoadPosition(group)
	self:UpdateLock()
end

function Wheel:UpdateAll()
	for group in pairs(self.anchor) do
		self:ApplySettings(group)
	end
end

function Wheel:SetColours(bar, class)
	if class then
		local c = RAID_CLASS_COLORS[class]
		bar:UnsetAllColors()
		bar:SetColorAt(1.00, c.r, c.g, c.b, 1)
	end
end

function Wheel:SetGroupColours()
	--[[
	local c1 = self.db.profile.colour1
	local c2 = self.db.profile.colour2
	local c3 = self.db.profile.colour3
	self.anchor:SetColorAt(1.00, c1.r, c1.g, c1.b, 1)
	self.anchor:SetColorAt(0.50, c2.r, c2.g, c2.b, 1)
	self.anchor:SetColorAt(0.00, c3.r, c3.g, c3.b, 1)
	]]
end

function Wheel:StartBar(group, name, class, spellID, duration)
	local spell, _, icon = GetSpellInfo(spellID)
	local barname = group.."-"..name.."-"..spellID
	local bar = self.anchor[group]:NewTimerBar(barname, name, duration, duration, icon)
	self:SetColours(bar, class)
end

function Wheel:StopBar(group, name, spellID)
	local barname = group.."-"..name.."-"..spellID
	self.anchor[group]:RemoveBar(barname)
end

function Wheel:Test(group)
	self:StartBar(group, "Paladin1", "PALADIN", 31821, 30)
	self:StartBar(group, "Priest", "PRIEST", 47788, 20)
	self:StartBar(group, "Paladin2", "PALADIN", 6940, 10)
end

function Wheel:IsGroupEnabled(name)
	return self.db.profile.groups[name] and self.db.profile.groups[name].enable
end


